<?php
class VASGroups extends Zend_Db_Table_Abstract
{
    protected $_name = 'VAS_groups';

	private $_errorCode = '';
	private $_errorStr = '';
	
	public function getErrorCode() { return $this->_errorCode; }
	public function getErrorStr() { return $this->_errorStr; }
	
	public function groupForBranchId( $branch_id )
	{
		$group_id = "";
		if(!isset($branch_id)) 
		{
			$this->_errorCode = 'INVALID_PARAMETER';
			$this->_errorStr = 'Missing or empty case number';
			return null;	
		}
		
		try
		{
			$row = $this->fetchRow("FDMS_branch_num=$branch_id");
			$group_id = $row->VAS_group_id;
		}
		catch(Exception $e)
		{
			$this->_errorCode = 'SQL_ERROR';
			$this->_errorStr = ''.$e;
			$group_id = "";	
		}
		
		return $group_id;
	}
	
	public function branchesForGroupId( $group_id )
	{
		$branch_ids = array();
		if(!isset($branch_id)) 
		{
			$this->_errorCode = 'INVALID_PARAMETER';
			$this->_errorStr = 'Missing or empty case number';
			return null;	
		}
		
		try
		{
			$rows = $this->fetchAll("VAS_group_id=$group_id");
			foreach( $rows as $row ) {
				$branch_id = $row->FDMS_branch_num;
				$branch_ids[] = $branch_id;
			}
		}
		catch(Exception $e)
		{
			$this->_errorCode = 'SQL_ERROR';
			$this->_errorStr = ''.$e;
			$items = null;	
		}
		
		return $branch_ids;
	}

	public function groupIdsForBranchIds( $branch_ids )
	{
		$group_ids = array();
		if(!isset($branch_ids)) 
		{
			$this->_errorCode = 'INVALID_PARAMETER';
			$this->_errorStr = 'Missing or empty branch id list';
			return null;	
		}
		
		$branch_id_list = "(" . implode( ",", $branch_ids ) . ")";
		try
		{
			$rows = $this->fetchAll("FDMS_branch_num IN $branch_id_list");
			foreach( $rows as $row ) {
				if(!in_array( $row->VAS_group_id, $group_ids ))
					$group_ids[] = $row->VAS_group_id;
			}
		}
		
		catch(Exception $e)
		{
			$this->_errorCode = 'SQL_ERROR';
			$this->_errorStr = ''.$e;
			$group_ids = null;	
		}
		
		return $group_ids;
	}
}
?>